<html>
<head><meta charset="utf-8"><title>MIR opts outside of the query system? · t-compiler/wg-mir-opt · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/index.html">t-compiler/wg-mir-opt</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR.20opts.20outside.20of.20the.20query.20system.3F.html">MIR opts outside of the query system?</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="219406347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR%20opts%20outside%20of%20the%20query%20system%3F/near/219406347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR.20opts.20outside.20of.20the.20query.20system.3F.html#219406347">(Dec 09 2020 at 22:10)</a>:</h4>
<p>Seeing the bug about the cycles caused by MIR inlining and const-eval, I was wondering: should we eventually move away from queries once MIR is built/checked, and use a manual dependency tracking using anonymous nodes like the type checker does?<br>
This would (1) avoid stolen MIR errors, (2) allow for multiple optimisation passes (fixed-point loop?), (3) allow to drop the HIR.<br>
On the other hand, this would require to explicit the MIR optimisation &amp; inlining global loop.</p>



<a name="219406985"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR%20opts%20outside%20of%20the%20query%20system%3F/near/219406985" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR.20opts.20outside.20of.20the.20query.20system.3F.html#219406985">(Dec 09 2020 at 22:15)</a>:</h4>
<p>That would probably be hard to integrate with the rest of the incremental system such that the optimized mir and compiled codegen units can still be cached.</p>



<a name="219407122"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR%20opts%20outside%20of%20the%20query%20system%3F/near/219407122" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR.20opts.20outside.20of.20the.20query.20system.3F.html#219407122">(Dec 09 2020 at 22:16)</a>:</h4>
<p>Anonymous nodes don't avoid dependency tracking. I think they just attribute the dependencies to the query that invoked the anonymous query.</p>



<a name="219408657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR%20opts%20outside%20of%20the%20query%20system%3F/near/219408657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR.20opts.20outside.20of.20the.20query.20system.3F.html#219408657">(Dec 09 2020 at 22:28)</a>:</h4>
<p>Oh, yes, I forgot about the caching. So no dropping the TyCtxt early.<br>
Given the list of red optimized_mir nodes, we would be able to construct a work list of the required optimization work queue. Treating it as a queue, would avoid having cycles. I mention anonymous nodes to track the dependencies each time the MIR for one item requires a previously optimised MIR.</p>



<a name="219409610"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR%20opts%20outside%20of%20the%20query%20system%3F/near/219409610" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> cjgillot <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR.20opts.20outside.20of.20the.20query.20system.3F.html#219409610">(Dec 09 2020 at 22:37)</a>:</h4>
<p>This is what I have in mind:</p>
<div class="codehilite"><pre><span></span><code>let mut items = FxHashMap&lt;DefId, (Mir, DepNode)&gt;;
let mut queue = Vec::new();
for def_id in all_def_ids {
  if /* optimised_mir is green */ {
    items.insert(def_id, (tcx.validated_mir(def_id), /* validated MIR dep_node */));
  } else {
    items.insert(def_id, (tcx.validated_mir(def_id), /* validated MIR dep_node */));
    queue.push(def_id);
  }
}
while !queue.is_empty() {
   let def_id = queue.pop();
  let (mir, old_node) = items[def_id];
  // create a new anonymous dep node `node`
  // add an edge `node -&gt; old_node`
  /* optimisation passes */
  // inlining
  for i_def_id in inlined_items {
    assert_ne!(def_id, i_def_id);
    // add a dependency edge `node -&gt; items[i_def_id].1`
  }
  items[def_id] = (mir, node);
  // reschedule if we have some more fuel or some dependencies are in the queue
}
</code></pre></div>



<a name="219410236"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/189540-t-compiler/wg-mir-opt/topic/MIR%20opts%20outside%20of%20the%20query%20system%3F/near/219410236" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/189540-t-compiler/wg-mir-opt/topic/MIR.20opts.20outside.20of.20the.20query.20system.3F.html#219410236">(Dec 09 2020 at 22:43)</a>:</h4>
<p>Using fuel is not compatible with incremental compilation. Any tiny change can cause all functions to be optimized differently.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>